class BetsController < ApplicationController
  
  # GET /bets
  # GET /bets.xml
  def index
    @bets = Bet.all
    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @bets }
    end
  end
  
  # Controlador de vista intermedia (confirmacion de pago)
  def paymentconfirm
  end
  
  # Controlador de vista intermedia (confirmacion de pago)
  def confirm
  end

  # GET /bets/1
  # GET /bets/1.xml
  def show
    @bet = Bet.find(params[:id])
    @user = User.find(session[:user_id])
    @paymethods = Paymethod.find_all_by_user_idf(@user.id)
    @eventparticipant = Eventparticipant.find(@bet.eventparticipant_idf)
    @event = Event.find(@eventparticipant.event_idf)
    @participant = Participant.find(@eventparticipant.participant_idf)
    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @bet }
      format.pdf do      
      	 render :pdf => "FacturaApuesta", :stylesheets => ["application", "prince"], :layout => "pdf"
  	  end
    end
  end

  # GET /bets/new
  # GET /bets/new.xml
  def new
    @bet = Bet.new
    @eventparticipant = Eventparticipant.find_by_id(params[:id])
    @event = Event.find_by_id(@eventparticipant.event_idf)
    @participant = Participant.find_by_id(@eventparticipant.participant_idf)
    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @bet }
    end
  end

  # GET /bets/1/edit
  def edit
    @bet = Bet.find(params[:id])
    @eventparticipant = Eventparticipant.find(params[:id])
    @event = Event.find(@eventparticipant.event_idf)
    @participant = Participant.find(@eventparticipant.participant_idf)
  end
  
 # Manejo conjunto de datos de apuesta y los metodos de pago del usuario
 def bill
    @bet = Bet.find(params[:id])
    @eventparticipant = Eventparticipant.find(params[:id])
    @event = Event.find(@eventparticipant.event_idf)
    @participant = Participant.find(@eventparticipant.participant_idf)
    @payment = Paymethod.find(params[:id2])
     respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @bet }
  	 end
  end

  # POST /bets
  # POST /bets.xml
  def create
    @bet = Bet.new(params[:bet])
    respond_to do |format|
      if @bet.save
        flash[:notice] = 'La apuesta a sido creada satisfactoriamente.'
        format.html { redirect_to(@bet) }
        format.xml  { render :xml => @bet, :status => :created, :location => @bet }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @bet.errors, :status => :unprocessable_entity }
      end
    end
  end

  # PUT /bets/1
  # PUT /bets/1.xml
  def update
    @bet = Bet.find(params[:id])
    respond_to do |format|
      if @bet.update_attributes(params[:bet])
        flash[:notice] = 'BLa apuesta a sido creada satisfactoriamente.'
        format.html { redirect_to(@bet) }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @bet.errors, :status => :unprocessable_entity }
      end
    end
  end

  # DELETE /bets/1
  # DELETE /bets/1.xml
  def destroy
    @bet = Bet.find(params[:id])
    @bet.destroy
    respond_to do |format|
      format.html { redirect_to(bets_url) }
      format.xml  { head :ok }
    end
  end
end
